const vm = require('vm')
const lsc = require('livescript')
const prelude = require('prelude-ls')
const lodash = require("lodash/fp")
const shelljs = require('shelljs')

function eval (code, context, file, cb){
    try {
        code = lsc.compile(code, {filename: file, bare: true})
        let script = new vm.Script(code)
        cb(null, script.runInContext(vm.createContext(context)))
    } catch (err) {
        cb(err)
    }
}

module.exports = ({repl, prompt, context, es}) => {
    repl = repl || require('repl')
    prompt = prompt || '|> '
    const r = repl.start({ prompt
                         , eval     : es ? undefined : eval
                         , useGlobal: true
                         })
    Object.assign( r.context
                 , shelljs
                 , { s: shelljs
                   , l: lodash
                   , p: prelude
                   }
                 , prelude
                 , context
                 , { cl: console.log.bind(console) }
                 )
    return r
}
